package lanQiaoBei.搜索与图论.最小生成树;
import java.io.*;
import java.math.*;
import java.util.*;
/*prim算法适用于稠密图求最小生成树(n^2)
*题目链接 https://blog.csdn.net/qq_45260619/article/details/115479209
* */
public class P1 {
       static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       final static int N=500;
       static int g[][]=new int[N][N],dist[]=new int[N],n,m;
       static boolean[]close=new boolean[N];

       static void scan()throws Exception{
              String[]ss=br.readLine().split(" ");
              n=Integer.parseInt(ss[0]); m=Integer.parseInt(ss[1]);
              for(int i=1;i<=n;i++)
                     Arrays.fill(g[i],0x3f3f3f3f);
              while(m-->0){
                    ss=br.readLine().split(" ");
                    int a=Integer.parseInt(ss[0]),b=Integer.parseInt(ss[1]),w=Integer.parseInt(ss[2]);
                    if(a!=b){
                           g[a][b]=g[b][a]=Math.min(g[a][b],w);
                    }
              }
       }
       static int prim(){
              Arrays.fill(dist,0x3f3f3f3f);
              int res=0;
              for(int i=0;i<n;i++){
                     int t=-1;
                     for(int j=1;j<=n;j++)
                            if(!close[j]&&(t==-1||dist[j]<dist[t]))
                                   t=j;

                     if(i!=0&&dist[t]==0x3f3f3f3f)return -1;
                     if(i!=0)res+=dist[t];
                     close[t]=true;

                     for(int j=1;j<=n;j++)dist[j]=Math.min(dist[j],g[t][j]);
              }
              return res;
       }
       public static void main(String[]args)throws Exception{
              scan();
              int t=prim();
              if(t==-1)System.out.print("impossible");
              else System.out.print(t);
       }
}
